package main

import (
	"github.com/gin-gonic/gin"
	"html/template"
	"io/fs"
	"net/http"
)

func main() {

	router := gin.Default()

	// 从嵌入文件系统加载模板
	templ := template.Must(template.New("").Funcs(router.FuncMap).ParseFS(TemplateFS, "templates/*.tmpl", "templates/**/*.tmpl"))
	router.SetHTMLTemplate(templ)
	//router.LoadHTMLGlob()

	// 配置静态web目录   第一个参数表示路由   第二个参数表示映射的目录
	// 这样你就可以通过: localhost:8080/static/css/base.css
	// 页面引入: <link rel="stylesheet" href="/static/css/base.css">
	//router.Static("/static", "./static")

	// 访问方式：http://ip:port/static/static/css/style.css  重复一个static路径
	//router.StaticFS("/static", http.FS(StaticFS))

	// 消除重复的static路径
	subFS, _ := fs.Sub(StaticFS, "static")
	router.StaticFS("/static", http.FS(subFS))

	router.GET("/hello", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"title": "首页",
			"score": 85,
			"hobby": []string{"吃饭", "睡觉", "写代码", "玩游戏"},

			"testSlice": []string{},

			"date": 1700636995,
			"msg":  "===",
		})
	})

	router.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "首页",
			"score": 85,
			"hobby": []string{"吃饭", "睡觉", "写代码", "玩游戏"},

			"testSlice": []string{},

			"date": 1700636995,
			"msg":  "===",
		})
	})

	userGroup := router.Group("/user")
	userGroup.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "user.tmpl", gin.H{
			"title": "首页",
			"score": 85,
			"hobby": []string{"吃饭", "睡觉", "写代码", "玩游戏"},

			"testSlice": []string{},

			"date": 1700636995,
			"msg":  "===",
		})
	})

	router.Run(":8080")
}
